package com.lisn.mylib;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import static com.google.android.material.bottomnavigation.BottomNavigationView.*;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private OnNavigationItemSelectedListener navItemSelectedListener = new OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int itemId = item.getItemId();
            Log.e(TAG, "onNavigationItemSelected: itemId = " + itemId);
            Log.e(TAG, "onNavigationItemSelected: itemId = " + item.getTitle());
            Log.e(TAG, "onNavigationItemSelected: itemId.toString = " + item.toString());
            if (tvTitle != null) {
                tvTitle.setText(item.getTitle());
            }
            if (navController != null) {
                return NavigationUI.onNavDestinationSelected(item, navController);
            }
            return false;
        }
    };
    private NavController navController;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initActionBar();

        BottomNavigationView navView = findViewById(R.id.nav_view);
        disableShiftMode(navView);


        navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard,
                R.id.navigation_rescue, R.id.navigation_setting)
                .build();
        /* 为AppBar设置导航控制器，监听导航改变事件，修改标题 */
        //NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        /* 为视图设置导航控制器，即监听视图的点击事件 */
        NavigationUI.setupWithNavController(navView, navController);


        /**
         * 为了手动控制更新标题
         * 覆盖重写了NavigationUI.setOnNavigationItemSelectedListener
         * 中的setOnNavigationItemSelectedListener方法
         */
        navView.setOnNavigationItemSelectedListener(navItemSelectedListener);
        /*去除默认图片选择*/
        navView.setItemIconTintList(null);
    }

    /**
     * 自定义布局ActionBar（标题居中）
     */
    @SuppressLint("WrongConstant")
    private void initActionBar() {
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);
        View viewTitleBar = getLayoutInflater().inflate(R.layout.action_bar_title, null);
        getSupportActionBar().setCustomView(viewTitleBar, lp);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        tvTitle = (TextView) getSupportActionBar().getCustomView().findViewById(android.R.id.title);
    }

    /**
     * 解决 BottomNavigationView > 3 标题隐藏问题
     * @param view
     */
    @SuppressLint("RestrictedApi")
    public static void disableShiftMode(BottomNavigationView view) {
        int count = view.getChildCount();
        if (count > 0) {
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
            if (menuView != null) {
                menuView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
                menuView.updateMenuView();
            }
        }
    }


}